package com.mall.service.authority.controller;

import com.mall.commons.common.CommonPage;
import com.mall.commons.common.CommonResult;
import com.mall.model.domain.UmsMenu;
import com.mall.model.domain.UmsResource;
import com.mall.model.domain.UmsRole;
import com.mall.service.authority.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author Pan Rundong
 * @Date 2023-03-12 14:29
 * @Desc 角色Controller
 */
@RestController
@RequestMapping("role")
@Slf4j
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 给角色分配菜单
     *
     * @param map
     * @return com.mall.commons.common.CommonResult<java.lang.String>
     * @Author Pan Rundong
     */
    @PostMapping("allocMenu")
    public CommonResult<String> allocMenu(@RequestParam Map map) {
        log.info("map = {}", map.toString());
        Long roleId = Long.valueOf(map.get("roleId").toString());
        String idsStr = map.get("menuIds").toString();
        String[] strings = idsStr.split(",");
        List<Long> menuIds = new ArrayList<>();
        for (String idStr : strings) {
            menuIds.add(Long.valueOf(idStr));
        }
        roleService.allocMenu(menuIds, roleId);
        return CommonResult.success("角色分配菜单成功");
    }

    /**
     * 给角色分配资源
     *
     * @param map
     * @return com.mall.commons.common.CommonResult<java.lang.String>
     * @Author Pan Rundong
     */
    @PostMapping("allocResource")
    public CommonResult<String> allocResource(@RequestParam Map map) {
        log.info("map = {}", map.toString());
        Long roleId = Long.valueOf(map.get("roleId").toString());
        String idsStr = map.get("resourceIds").toString();
        String[] strings = idsStr.split(",");
        List<Long> resourceIds = new ArrayList<>();
        for (String idStr : strings) {
            resourceIds.add(Long.valueOf(idStr));
        }
        roleService.allocResource(resourceIds, roleId);
        return CommonResult.success("角色分配菜单成功");
    }

    /**
     * 分页获取角色列表
     *
     * @param
     * @return com.mall.commons.common.CommonPage<com.mall.model.domain.UmsRole>
     * @Author Pan Rundong
     */
    @GetMapping("list")
    public CommonResult<CommonPage<UmsRole>> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                                  String keyword)
    {
        log.info("pageNum = {}, pageSize = {}, keyword = {}", pageNum, pageSize, keyword);
        CommonPage<UmsRole> page = roleService.findByPage(pageNum, pageSize, keyword);
        return CommonResult.success(page, "获取角色列表成功");
    }

    /**
     * 添加角色
     *
     * @param umsRole
     * @return com.mall.commons.common.CommonResult<java.lang.String>
     * @Author Pan Rundong
     */
    @PostMapping("create")
    public CommonResult<String> create(@RequestBody UmsRole umsRole) {
        log.info("role = {}", umsRole.toString());
        Boolean flag = roleService.checkAndSave(umsRole);
        if (flag) {
            return CommonResult.success(null, "角色添加成功");
        } else {
            return CommonResult.error("角色重名");
        }
    }

    /**
     * 更新角色
     *
     * @param umsRole
     * @return com.mall.commons.common.CommonResult<java.lang.String>
     * @Author Pan Rundong
     */
    @PostMapping("update/{id}")
    public CommonResult<String> update(@PathVariable Long id, @RequestBody UmsRole umsRole) {
        log.info("role = {}", umsRole.toString());
        Boolean flag = roleService.checkAndUpdate(id, umsRole);
        if (flag) {
            return CommonResult.success(null, "角色修改成功");
        } else {
            return CommonResult.error("角色修改失败");
        }
    }

    /**
     * 批量删除
     *
     * @param
     * @return com.mall.commons.common.CommonResult<java.lang.String>
     * @Author Pan Rundong
     */
    @PostMapping("delete")
    public CommonResult<String> delete(@RequestParam List<Long> ids) {
        log.info("ids = {}", ids);
        return roleService.deleteByIds(ids);
    }

    /**
     * 获取全部角色
     *
     * @param
     * @return com.mall.commons.common.CommonResult<java.util.List < com.mall.model.domain.UmsRole>>
     * @Author Pan Rundong
     */
    @GetMapping("listAll")
    public CommonResult<List<UmsRole>> listAll() {
        log.info("获取全部角色");
        List<UmsRole> umsRoles = roleService.list();
        return CommonResult.success(umsRoles, "获取角色成功");
    }

    /**
     * 根据用户Id获取对应菜单
     *
     * @param
     * @return com.mall.commons.common.CommonResult<java.util.List < com.mall.model.domain.UmsMenu>>
     * @Author Pan Rundong
     */
    @GetMapping("listMenu/{roleId}")
    public CommonResult<List<UmsMenu>> listMenu(@PathVariable Long roleId) {
        log.info("roleId = {}", roleId);
        List<UmsMenu> menuList = roleService.listMenu(roleId);
        return CommonResult.success(menuList);
    }

    /**
     * 根据用户Id获取对应后台资源
     *
     * @param
     * @return com.mall.commons.common.CommonResult<java.util.List < com.mall.model.domain.UmsMenu>>
     * @Author Pan Rundong
     */
    @GetMapping("listResource/{roleId}")
    public CommonResult<List<UmsResource>> listResource(@PathVariable Long roleId) {
        log.info("roleId = {}", roleId);
        List<UmsResource> menuList = roleService.listResource(roleId);
        return CommonResult.success(menuList);
    }

    /**
     * 修改状态
     *
     * @param id
     * @param status
     * @return com.mall.commons.common.CommonResult<java.lang.String>
     * @Author Pan Rundong
     */
    @PostMapping("updateStatus/{id}")
    public CommonResult<String> updateStatus(@PathVariable Long id, Integer status) {
        log.info("id = {}, status = {}", id, status);
        roleService.updateStatus(id, status);
        return CommonResult.success(null,"更改状态成功");
    }
}
